%segmentPN('images\p1_im.nii','a.nii');
%[a,b,c] = evaluateSegmentaion('images\p1_gt.nii','a.nii');
%c

i = load_nifti('my_bla.nii');

image = i;

a = strel('diamond',1);

for i=1:size(image,3)
    sl = image(:,:,i);
    sl = imdilate(sl,a);
    sl = imerode(sl,a);
    sl = imerode(sl,a);
    sl = imdilate(sl,a);
    sl = imdilate(sl,a);
    sl = imdilate(sl,a);
    
    
    CC = bwconncomp(sl,8);
    
    PixelIdxList = CC.PixelIdxList;
    
    if (size(PixelIdxList,2)==0)
        image(:,:,i) = sl;
        continue;
    end
    
     for j=1:length(PixelIdxList)    
        
        s = length(PixelIdxList{j});
        
        if (s < 50)
            sl(PixelIdxList{j})=0;
        end
        
        
    end
    
    
    
    image(:,:,i) = sl;
end

save_nifti(image,'my_improved_bla.nii',2);

